﻿using RpcStore.Collect.LocalEvent.Model;
using RpcStore.DAL;
using RpcStore.Model.DB;
using RpcStore.Model.TransmitScheme;
using RpcStore.RemoteModel.TransmitScheme.Model;
using WeDonekRpc.Client;
using WeDonekRpc.Helper;
using WeDonekRpc.Model;
using WeDonekRpc.SqlSugar;

namespace RpcStore.Collect.lmpl
{
    internal class ServerTransmitSchemeCollect : IServerTransmitSchemeCollect
    {
        private readonly IServerTransmitSchemeDAL _Scheme;
        private readonly IServerTransmitConfigDAL _Config;
        private readonly ITransactionService _Tran;

        public ServerTransmitSchemeCollect (IServerTransmitSchemeDAL scheme,
            IServerTransmitConfigDAL config,
            ITransactionService tran)
        {
            this._Scheme = scheme;
            this._Config = config;
            this._Tran = tran;
        }

        public long Add (TransmitSchemeAdd scheme)
        {
            if (scheme.IsAuto)
            {
                this._Scheme.CheckIsRepeat(scheme.RpcMerId, scheme);
            }
            return this._Scheme.Add(scheme);
        }


        public void Delete (ServerTransmitSchemeModel scheme)
        {
            if (scheme.IsEnable)
            {
                throw new ErrorException("rpc.store.transmit.already.enable");
            }
            this._Scheme.Delete(scheme.Id);
            new DeleteTransmitScheme
            {
                Scheme = scheme
            }.AsyncPublic();
        }

        public ServerTransmitSchemeModel Get (long id)
        {
            return this._Scheme.Get(id);
        }

        public ServerTransmitSchemeModel[] Query (TransmitSchemeQuery query, IBasicPage paging, out int count)
        {
            return this._Scheme.Query(query, paging, out count);
        }

        public bool SetIsEnable (ServerTransmitSchemeModel scheme, bool isEnable)
        {
            if (scheme.IsEnable == isEnable)
            {
                return false;
            }
            else if (isEnable && scheme.IsAuto == false)
            {
                if (!this._Config.CheckIsExists(scheme.Id))
                {
                    throw new ErrorException("rpc.store.transmit.null");
                }
            }
            if (isEnable)
            {
                this._Scheme.CheckIsRepeatEnable(scheme);
            }
            this._Scheme.SetIsEnable(scheme.Id, isEnable);
            return true;
        }

        public void SetScheme (ServerTransmitSchemeModel scheme, TransmitSchemeSet set)
        {
            if (set.IsEquals(scheme))
            {
                return;
            }
            else if (set.IsAuto)
            {
                this._Scheme.CheckIsRepeat(scheme.RpcMerId, set);
            }
            TransmitSchemeSetDatum datum = set.ConvertMap<TransmitSchemeSet, TransmitSchemeSetDatum>();
            if (!scheme.IsAuto && ( set.TransmitType != scheme.TransmitType
                || set.IsAuto != scheme.IsAuto
                || set.SystemTypeId != scheme.SystemTypeId ))
            {
                long[] ids = this._Config.GetIds(scheme.Id);
                if (!ids.IsNull())
                {
                    using (ILocalTransaction tran = this._Tran.ApplyTran())
                    {
                        this._Scheme.SetScheme(scheme.Id, datum);
                        this._Config.Delete(ids);
                        tran.Commit();
                    }
                    return;
                }
            }
            this._Scheme.SetScheme(scheme.Id, datum);
        }
    }
}
